let app = getApp()

const Store = function(obj, useStates = {}) {
    return new Proxy(app.globalData, {
        set(target, key, value) {
            if (Array.isArray(useStates) && useStates.includes(key)) {
                obj.setData({
                    [key]: value
                })
            } else if (useStates instanceof Object && useStates.hasOwnProperty(key)) {
                obj.setData({
                    [key]: value
                });
            }
            return Reflect.set(target, key, value)
        },
        get(target, key) {
            return Reflect.get(target, key)
        }
    })
}

export const bindStore = function(obj, useStates = {}) {
    let tmpData = {}
    if (Array.isArray(useStates)) {
        for (let i in useStates) {
            tmpData[useStates[i]] = app.globalData[useStates[i]]
        }
        obj.setData(tmpData)
    } else if (useStates instanceof Object) {
        for (let i in useStates) {
            tmpData[i] = app.globalData[useStates[i]]
        }
        obj.setData(tmpData)
    }
    return Store(obj, useStates)
}